//
//  LoginViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPassWordViewController.h"
#import "LoginViewModel.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *passwordLabel;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _loginButton.layer.cornerRadius = 5;
    [_phoneLabel becomeFirstResponder];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *iphone = [userDefaults objectForKey:@"iphone_number"];
    if (!iphone && iphone.length > 0) {
        _phoneLabel.text = iphone;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_passwordLabel resignFirstResponder];
    [_phoneLabel resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_login:(id)sender {
    if ([_phoneLabel.text length]!=11) {
    
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];

        return;
    }
    if ([_passwordLabel.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_phoneLabel.text forKey:@"iphone_number"];
    __weak LoginViewController *weakSelf = self;
    [LoginViewModel loginWithUser:_phoneLabel.text
                             type:@"3"
                         password:_passwordLabel.text
                          success:^{
                              [weakSelf dismissViewControllerAnimated:YES
                                                           completion:nil];
    }
                          failure:^(NSString *message) {
                              
                              dispatch_queue_t mainQueue = dispatch_get_main_queue();
                              dispatch_async(mainQueue, ^(void) {
                                  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                  [alertView show];
                              });
                             
                          }];
    
    
}
- (IBAction)p_forgetPassword:(id)sender {
    ForgetPassWordViewController *registerVC = [[ForgetPassWordViewController alloc] initWithNibName:NSStringFromClass([ForgetPassWordViewController class]) bundle:nil];
    [self presentViewController:registerVC animated:YES completion:nil];
}
- (IBAction)p_register:(id)sender {
    RegisterViewController *registerVC = [[RegisterViewController alloc] initWithNibName:NSStringFromClass([RegisterViewController class]) bundle:nil];
    [self presentViewController:registerVC animated:YES completion:nil];
   
}

@end
